"""
@Author  : 康帅
@Time    : 2021/3/23 15:02
@Function: 组合模式基类
"""
from abc import ABCMeta, abstractmethod


class Component(metaclass=ABCMeta):
    """ 组件 """

    def __init__(self, name):
        self._name = name

    def get_name(self):
        return self._name

    def is_composite(self):
        return False

    @abstractmethod
    def feature(self, indent):
        # indent 仅用于缩进
        pass


class Composite(Component):
    """ 复合组件 """

    def __init__(self, name):
        super().__init__(name)
        self._components = []

    def add_components(self, component):
        self._components.append(component)

    def remove_components(self, component):
        self._components.remove(component)

    def is_composite(self):
        return True

    def feature(self, indent):
        indent += "\t"
        for component in self._components:
            print(component, end="")
            component.feature(indent)
